package com.guli.product.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guli.common.utils.Constant;
import com.guli.common.utils.PageUtils;
import com.guli.product.dao.CategoryBrandRelationDao;
import com.guli.product.entity.CategoryBrandRelationEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guli.product.dao.BrandDao;
import com.guli.product.entity.BrandEntity;
import com.guli.product.service.BrandService;
import org.springframework.util.StringUtils;


@Service("brandService")
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    @Autowired
    private CategoryBrandRelationDao categoryBrandRelationDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<BrandEntity> page = baseMapper
                .selectPage(new Page<>(Long.parseLong((String) params.get(Constant.PAGE)),
                                Long.parseLong((String) params.get(Constant.LIMIT))),
                        new QueryWrapper<BrandEntity>()
                                .like(!StringUtils.isEmpty(params.get(Constant.KEY)), "name", params.get(Constant.KEY))
                                .orderByAsc("sort"));
        return new PageUtils(page);
    }

    @Override
    public void updateBrandWithRelation(BrandEntity brand) {
        //更新该品牌信息
        baseMapper.updateById(brand);
        //更新与该品牌关联的信息
        categoryBrandRelationDao.updateRelationBrand(brand.getBrandId(), brand.getName());
    }

    @Override
    public void removeBrandWithRelation(List<Long> brandIds) {
        //删除该品牌信息
        baseMapper.deleteBatchIds(brandIds);
        //删除与该品牌关联的信息
        categoryBrandRelationDao.delete(new QueryWrapper<CategoryBrandRelationEntity>()
                .in("brand_id",brandIds));
    }
}